package com.taa.controller;

import com.taa.dao.po.DishInfo;
import com.taa.dto.DishInfoDTO;
import com.taa.dto.DishStockDTO;
import com.taa.dto.RestaurantInfoDTO;
import com.taa.enums.ResponseCode;
import com.taa.exception.AppException;
import com.taa.response.Response;
import com.taa.service.IRestaurantService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: taa
 * @Desc: 商家服务控制层
 * @Create: 2025-06-22 16:42
 **/
@Slf4j
@RestController
@RequestMapping("/restaurant") // 对应网关转发后的路径 /api/restaurant/**
public class RestaurantController {

    @Resource
    private IRestaurantService restaurantService;

    /**
     * 获取商家列表接口
     * @return Response<List<RestaurantInfoDTO>>
     */
    @GetMapping("/list")
    public Response<List<RestaurantInfoDTO>> getRestaurantList() {
        try {
            log.info("获取商家列表请求开始");
            List<RestaurantInfoDTO> restaurantList = restaurantService.getRestaurantList();
            log.info("获取商家列表成功, 数量: {}", restaurantList.size());
            return Response.<List<RestaurantInfoDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(restaurantList)
                    .build();
        } catch (Exception e) {
            // 此处通常不会有AppException，因为查询操作不抛出业务异常
            log.error("获取商家列表系统异常", e);
            return Response.<List<RestaurantInfoDTO>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 根据商家ID获取菜品列表接口
     * @param restaurantId 商家ID
     * @return Response<List<DishInfoDTO>>
     */
    @GetMapping("/{restaurantId}/dishes")
    public Response<List<DishInfoDTO>> getDishes(@PathVariable Long restaurantId) {
        try {
            log.info("获取菜品列表请求开始, 商家ID: {}", restaurantId);
            List<DishInfoDTO> dishList = restaurantService.getDishesByRestaurantId(restaurantId);
            log.info("获取菜品列表成功, 商家ID: {}, 数量: {}", restaurantId, dishList.size());
            return Response.<List<DishInfoDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(dishList)
                    .build();
        } catch (AppException e) {
            log.error("获取菜品列表业务异常, 商家ID: {}", restaurantId, e);
            return Response.<List<DishInfoDTO>>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("获取菜品列表系统异常, 商家ID: {}", restaurantId, e);
            return Response.<List<DishInfoDTO>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
    
    /**
     * 根据ID列表批量查询菜品信息
     * @param ids
     * @return
     */
    @GetMapping("/dish/list-by-ids")
    public Response<List<DishInfo>> getDishListByIds(@RequestParam("ids") List<Long> ids) {
        try {
            List<DishInfo> dishList = restaurantService.getDishListByIds(ids);
            return Response.<List<DishInfo>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(dishList)
                    .build();
        } catch (Exception e) {
            log.error("内部接口-批量查询菜品异常", e);
            return Response.<List<DishInfo>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    
    /**
     * 批量扣减库存
     * @param dishStockList
     * @return
     */
    @PostMapping("/dish/decrease-stock")
    public Response<Void> decreaseStock(@RequestBody List<DishStockDTO> dishStockList) {
        try {
            restaurantService.decreaseStock(dishStockList);
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (AppException e) {
            log.error("内部接口-扣减库存业务异常", e);
            return Response.<Void>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("内部接口-扣减库存系统异常", e);
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
}

